﻿#region Used Namespaces

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AutoUnzip.Interfaces;

#endregion

namespace AutoUnzip.Core
{
    public class FileService : IFileService
    {
        public IEnumerable<FileInfo> FindZipFiles(string directory, string filter)
        {
            var directoryInfo = new DirectoryInfo(directory);
            if (!directoryInfo.Exists)
                return Enumerable.Empty<FileInfo>();

            string[] filters = filter.Split(';');
            IEnumerable<FileInfo> enumerateFiles = directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories);

            var result = from file in enumerateFiles
                         where filters.Contains(file.Extension.TrimStart('.'))
                         select file;
            return result;
        }

        public double GetFolderSize(DirectoryInfo fileInfo)
        {
            double totalSize = fileInfo.GetDirectories().Sum(directory => GetFolderSize(directory));
            totalSize += fileInfo.GetFiles().Sum(file => file.Length);

            return totalSize;
        }

        public async Task DeleteFilesAsync(IEnumerable<string> files)
        {
            foreach (var file in files)
            {
                await Task.Run(() => File.Delete(file));
            }
        }

        //public async Task MoveDirectoyAsync(string sourcePath, string destinationPath, IProgress<long> progress)
        //{
        //    var baseDirectory = new DirectoryInfo(sourcePath);
            
        //    var destPath = Path.Combine(destinationPath, baseDirectory.Name);
        //    var destInfo = new DirectoryInfo(destPath);
        //    if (!destInfo.Exists)
        //        destInfo.Create();

        //    var sourceDirectories = baseDirectory.GetDirectories("*", SearchOption.AllDirectories);
        //    //Now Create all of the directories
        //    foreach (var dirPath in sourceDirectories)
        //    {
        //        var destinationDirectory = Path.Combine(destPath, dirPath.Name);
        //        if (!Directory.Exists(destinationDirectory))
        //            Directory.CreateDirectory(destinationDirectory);
        //    }

        //    //Copy all the files
        //    var allFiles = baseDirectory.GetFiles("*.*", SearchOption.AllDirectories);
        //    var totalSize = allFiles.Sum(file => file.Length);
        //    var currentSize = 0;
        //    foreach (var file in allFiles)
        //    {
        //        using (var sourceReader = File.OpenRead(file.FullName))
        //        {
        //            //TODO: wrong path!
        //            var destFile = file.FullName.Replace(sourcePath, destInfo.FullName);
        //            using (var destinationWriter = File.Create(destFile))
        //            {
        //                await CopyFilesAsync(sourceReader, destinationWriter, p =>
        //                    {
        //                        currentSize += p;
        //                        progress.Report((currentSize / totalSize) * 100);
        //                    });
        //            }
        //        }
        //    }
        //}

        public async Task CopyFilesAsync(Stream source, Stream destination, Action<int> progress)
        {
            var buffer = new byte[0x1000];
            int numRead;
            while ((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) != 0)
            {
                await destination.WriteAsync(buffer, 0, numRead);
                progress(numRead);
            }
        }
    }
}
